﻿/*
 * Copyright (c) Seagull Consulting, Inc. All rights reserved. See License.txt in the project root for license information.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SeagullConsulting.MvcAjaxControls.Data.Models;
using SeagullConsulting.MvcAjaxControls.Data.Repository;
using SeagullConsulting.MvcAjaxControls.Web.UI.Helpers;

namespace SeagullConsulting.MvcAjaxControls.Web.UI.Controllers
{
    public class AjaxGridController : Controller
    {
        private PersonRepository personRepository;
        private CityRepository cityRepository;

        public AjaxGridController()
        {
            personRepository = new PersonRepository();
            cityRepository = new CityRepository();
            ViewData["controller"] = typeof(AjaxGridController).Name;
        }

        #region Index
        //
        // GET: /AjaxGrid/

        public ActionResult Index()
        {
            return View("Index");
        }
        #endregion

        #region ListPage
        //
        // GET: /AjaxGrid/ListPage or /AjaxGrid/ListPage/{id}

        public ActionResult ListPage(int? id)
        {
                return View("ListPage");
        }

        public JsonResult ListPerson(int pageNbr, int pageSize)
        {
            PagerParms parms;
            IEnumerable<Person> people;

            try
            {
                if (pageSize > 0)
                {
                    //Paging is being used
                    parms = new PagerParms() { PageSize = (int)pageSize };
                    if (pageNbr > 1)
                        parms.PageNbr = (int)pageNbr - 1;
                    else
                        parms.PageNbr = 0;
                    people = personRepository.List(parms);
                }
                else
                {
                    //Paging not being used.
                    people = personRepository.List();
                }
                return Json(people, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message + ex.StackTrace);
            }
        }

        public JsonResult FilterPerson(int pageNbr, int pageSize, string property, string propertyValue)
        {
            PagerParms parms;
            IEnumerable<Person> people;
            ExpressionBuilder<Person> expressionBuilder = new ExpressionBuilder<Person>(property, propertyValue);
            Func<Person, bool> expression = expressionBuilder.Where(WhereOperation.Contains);

            try
            {
                if (pageSize > 0)
                {
                    //Paging is being used
                    parms = new PagerParms() { PageSize = (int)pageSize };
                    if (pageNbr > 1)
                        parms.PageNbr = (int)pageNbr - 1;
                    else
                        parms.PageNbr = 0;
                    people = personRepository.List(parms, expression);
                }
                else
                {
                    //Paging not being used.
                    people = personRepository.List(expression);
                }
                return Json(people, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message + ex.StackTrace);
            }
        }

        public JsonResult ListPersonCount()
        {
            try
            {
                return Json(personRepository.ListCount(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message + ex.StackTrace);
            }
        }

        public JsonResult FilterPersonCount(string property, string propertyValue)
        {
            ExpressionBuilder<Person> expressionBuilder = new ExpressionBuilder<Person>(property, propertyValue);
            Func<Person, bool> expression = expressionBuilder.Where(WhereOperation.Contains);

            try
            {
                return Json(personRepository.ListCount(expression), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region Details
        //
        // GET: /AjaxGrid/Details/{id}

        public ActionResult Details(int id)
        {
            return View("Details", personRepository.GetById(id));
        }
        #endregion

    }
}
